<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>Convert CSV to Google Datatable</title>
  
    <style>
      body {font: 10pt arial;}
    </style>
                                                                        
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript" src="csv2array.js"></script>
    
    <script type="text/javascript">
      var data = null;
      var graph = null;
    
      function loaded() {
        
      }
    
      google.load("visualization", "1");
      
      // Set callback to run when API is loaded
      google.setOnLoadCallback(loaded); 

      // Called when the Visualization API is loaded.
      function convert() {
        var csv = document.getElementById("csv").value;
        
        var datatable = "";
        
        // parse the csv content
        var csvArray = csv2array(csv);

        // Create and populate a data table.
        datatable += "data = new google.visualization.DataTable();\n";
        
        // read the header row
        for (var col = 0; col < csvArray[0].length; col++) {
          datatable += "data.addColumn('number', '" + csvArray[0][col] + "');\n";
        }

        // read all data
        for (var row = 1; row < csvArray.length; row++) {
          datatable += "data.addRow([";
          
          for (var col = 0; col < csvArray[row].length; col++) {
            if (col != 0)
              datatable += ", ";
            datatable += csvArray[row][col];
          }
          
          datatable += "]);\n";
        }
        
        document.getElementById("datatable").value = datatable;
        
        alert(csvArray.length + " rows converted");
      }
   </script>
  </head>

  <body>
    <div id="graph"></div>

    <div id="info"></div>

    <b>CSV</b><br>
    <textarea id="csv" style="width: 400px; height: 300px;"></textarea>
    <br>
    <br>
    <input type="button" value="Convert" onclick="convert();">
    <br>
    <br>
    
    <b>Google DataTable</b><br>
    <textarea id="datatable" style="width: 400px; height: 300px;"></textarea>

  </body>
</html>
